package com.zhengaz.common.rabbitmq.config;

import com.zhengaz.common.rabbitmq.properties.MqProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhengzg
 * @create 2023-09-15 17:37
 */
@Slf4j
@Configuration
public class MqConfig {

    @Bean
    public MessageConverter messageConverter() {
        // json字符串转换器
        return new Jackson2JsonMessageConverter();
    }


    @Bean
    public Queue cloudReportTopicQueue() {
        return new Queue(MqProperties.QUEUE);
    }

    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(MqProperties.EXCHANGE);
    }

    /**
     * 绑定路由键，交换机，与队列
     */
    @Bean
    public Binding bindingExchangeMessage() {
        return BindingBuilder.bind(cloudReportTopicQueue()).to(exchange()).with(MqProperties.ROUTER_KEY);
    }

    @Bean
    @ConditionalOnMissingBean
    public MqProperties mqProperties() {
        return new MqProperties();
    }
}
